
package com.wyzz.hr.action;

import java.io.*;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.*;
import org.directwebremoting.WebContext;
import org.directwebremoting.WebContextFactory;

import com.wyzz.hr.util.impl.PropertiesFileConfigManager;

public class ShowImage extends HttpServlet {

	public ShowImage() {
	}

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		if (request.getSession().getAttribute("userinfo") == null) {
			response.sendRedirect((new StringBuilder()).append(
					request.getContextPath())
					.append("/configuration/login.jsp").toString());
			return;
		}
		String style = request.getParameter("style");
		if ("head".equalsIgnoreCase(style))
			showUserHead(request, response);
		else if ("news".equalsIgnoreCase(style)) {
			showNewsPic(request, response);
		} else {
			response.sendRedirect((new StringBuilder()).append(
					request.getContextPath())
					.append("/configuration/login.jsp").toString());
			return;
		}
	}

	private void showUserHead(HttpServletRequest request,
			HttpServletResponse response) {
		String fileName = request.getParameter("fileName");
		try {
			if (fileName == null || "".equals(fileName.trim()))
				fileName = "None.JPG";
			FileInputStream uFile = getFileInputStream(request,
					"sys.user.image.path", fileName);
			if (uFile == null)
				uFile = new FileInputStream((new StringBuilder()).append(
						request.getSession().getServletContext().getRealPath(
								"/")).append("resource/images/None.JPG")
						.toString());
			int i = uFile.available();
			byte data[] = new byte[i];
			uFile.read(data);
			uFile.close();
			response.setContentType("image/*");
			OutputStream toClient = response.getOutputStream();
			toClient.write(data);
			toClient.close();
		} catch (IOException e) {
		}
	}

	private void showNewsPic(HttpServletRequest request,
			HttpServletResponse response) {
		String fileName = request.getParameter("fileName");
		if (fileName == null || fileName.trim().equals(""))
			return;
		FileInputStream uFile;
		uFile = getFileInputStream(request, "sys.information.image.path",
				fileName);
		if (uFile == null)
			return;
		try {
			int i = uFile.available();
			byte data[] = new byte[i];
			uFile.read(data);
			uFile.close();
			response.setContentType("image/*");
			OutputStream toClient = response.getOutputStream();
			toClient.write(data);
			toClient.close();
		} catch (IOException e) {
		}
		return;
	}

	private String getFileHomePath(HttpServletRequest request) {
		if (request == null)
			request = WebContextFactory.get().getHttpServletRequest();
		String path = request.getSession().getServletContext().getRealPath("");
		File file = new File(path);
		if (!file.exists()) {
			return (new StringBuilder()).append(path).append("/file/")
					.toString();
		} else {
			// String newPath = file.getParentFile().getAbsolutePath();
			return (new StringBuilder()).append(path).append("/file/")
					.toString();
		}
	}

	private File getFile(HttpServletRequest request, String filePathConfig,
			String fileName) {
		File file;
		String filePath = null;
		if (filePathConfig != null)
			filePath = (new StringBuilder()).append(getFileHomePath(request))
					.append(
							PropertiesFileConfigManager.getInstance()
									.getProperty(filePathConfig)).toString();
		else
			filePath = getFileHomePath(request);
		file = new File((new StringBuilder()).append(filePath).append(fileName)
				.toString());
		System.out.println(file+"jjjjjjjj");
		if (!file.exists())
			return null;
		try {
			return file;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	private FileInputStream getFileInputStream(HttpServletRequest request,
			String filePathConfig, String fileName) {
		File file;
		file = getFile(request, filePathConfig, fileName);
		System.out.println(file+"ooooooooo");
		if (file == null)
			return null;
		try {
			return new FileInputStream(file);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	private static final long serialVersionUID = 1L;
}
